<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <!-- 整个案例可以分为三个功能模块
鼠标经过小图片盒子，黄色的遮挡层和大图片盒子显示，离开隐藏2个盒子功能
黄色的遮挡层跟随鼠标功能。
移动黄色遮挡层，大图片跟随移动功能。 -->
    <div class="container">
        <div class="small-box">
            <div class="mask"></div>
        </div>
        <div class="big-box"></div>
    </div>
    <style>
        /* CSS styles remain unchanged */
        .container {
            position: relative;
            width: 500px;
            height: 500px;
            margin: 0 auto;
        }
       .small-box {
            position: absolute;
            top: 0;
            left: 0;
            width: 100px;
            height: 100px;
            background-color: #e40000;
            border: 1px solid #ccc;
            cursor: pointer;
        }
       .big-box {
            position: absolute;
            top: 0;
            left: 0;
            width: 400px;
            height: 400px;
            background-color: #5a0101;
            border: 1px solid #ccc;
            display: none;
        }
        .mask {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(255, 0.5);
            display: none;
        }
    </style>
    <script>
        try {
            var smallBox = document.querySelector('.small-box');
            var bigBox = document.querySelector('.big-box');
            var mask = document.querySelector('.mask');
            var bigBoxWidth = bigBox.offsetWidth;
            var bigBoxHeight = bigBox.offsetHeight;

            // Show/hide boxes on hover
            smallBox.addEventListener('mouseenter', function() {
                bigBox.style.display = 'block';
                mask.style.display = 'block';
            });
            smallBox.addEventListener('mouseleave', function() {
                bigBox.style.display = 'none';
                mask.style.display = 'none';
            });

            // Move big box with mask
            mask.addEventListener('mousemove', function(e) {
                var rect = smallBox.getBoundingClientRect();
                var x = e.clientX - rect.left - (bigBoxWidth / 2);
                var y = e.clientY - rect.top - (bigBoxHeight / 2);

                // Keep bigBox within the container bounds
                x = Math.max(0, Math.min(x, 400 - bigBoxWidth));
                y = Math.max(0, Math.min(y, 400 - bigBoxHeight));

                bigBox.style.left = x + 'px';
                bigBox.style.top = y + 'px';
            });
        } catch (error) {
            console.error("An error occurred:", error);
        }
    </script>
</body>
</html>
